/**
 * @author benny.zheng
 * @data 2016-06-06
 * @description 本文件用于方便复制粘贴UI模块之用，请更新这里的说明
 *              另外，考虑到一般是放在js/src/pages/page-name/main.js，因此使用../../
 *              如果不是这个目录，请更改成正确的相对路径
 */
//----------------require--------------


var base = require("mlib/comp/base"); // 基础对象
var parseModule = require("mlib/dom/parseModule"); // 页面模块自动解析
var sizzle = require("lib/dom/sizzle");
var ajax = require("mlib/io/ajax");
//var wx = require("mlib/util/jweixin");
var closest = require("mlib/dom/closest");
var touch = require("mlib/evt/touch");
//var virtualLink = require("mlib/util/virtualLink");
var validator = require("mlib/util/validator");
var showMsg = require("../layer/info");
var getFormData = require("mlib/util/get-form-data"); 
var browser = require("lib/util/browser");
var alimsg = null;
module.exports = function(node, opts) {
//-----------声明模块全局变量-------------
    var nodeList = null; // 存储所有关键节点
    var that = base();
    var data = null;
	//-----------声明其他全局变量-------------
	var v = null;
	var formData = null;
    //-------------事件响应声明---------------
    var evtFuncs = {
		input: function(ev) {
			if(!browser.IPHONE && !browser.ITOUCH && !browser.IPAD && !browser.SAFARI){
				nodeList.lottery.style.webkitTransform = "translate3d(0,-10rem,0);" 
			}
		},
		getFocus:function(){
			document.body.style.height = document.documentElement.clientHeight;
		},
         submitInfo:function(e){
			if(that.isLock())return;
     		that.lock();
            v.validate();
		 }
    }

    //-------------子模块实例化---------------
    var initMod = function() {
    	var title = document.getElementsByTagName("title")[0];
    	title.innerHTML = "奖品兑换";
		v = new validator("info-form",[
			{
				//name 字段
				name: 'mobile',
				display:"请输入你的手机号码|手机号码格式不正确",
				// 验证条件
				rules: 'required|is_phone'
			},
            {
                //name 字段
                name: 'idcard',
                display:"请输入身份证|身份证错误",
                // 验证条件
                rules: 'required|is_idcard'
            }
		],function(obj,evt){	
			  if(obj.errors.length>0){
			  	 showMsg(obj.errors[0].message,null,function(){
			  	 	that.unLock();
			  	 });
			  }else{  
			        _czc.push(["_trackEvent", "活动", "兑换奖品", "点击兑换奖品"]);
					formData = getFormData(nodeList.infoform); 
					 ajax({// 验证通过后提交注册
						url: "/index.php?r=activity/receive", 
						data: {
								"mobile":formData.mobile,
								"idcard":formData.idcard,
								"cardid":formData.cardid,
								"cardcode":formData.cardcode
							},
						method: "post",
						onSuccess: function(res) {
							if(res.status == 1){
								location.href= res.data.url;
								that.unLock();
							}else{
								showMsg(res.msg,null,function(){
									that.unLock();
								});
							}
						},
						onError: function(xmlHttp) {
							 that.unLock();
						}, // 当访问出错，比如网络连接不上、解析内容失败时触发，超时也会触发
						onTimeout: function(xmlHttp) { }, // 超时触发
						onAbort: function() {} // 网络中断时触发
					 });
			  }
		  
		});
		

    }

    //-------------绑定事件------------------
    var bindEvents = function() { 
	   touch.on(nodeList.submitbtn,"click",evtFuncs.submitInfo);
	   touch.on(nodeList.mobile, 'tap', evtFuncs.input);
	   touch.on(nodeList.mobile,"blur",evtFuncs.getFocus);
    }

    //-------------自定义函数----------------
    var custFuncs = {
		taskTimer: function(){
			if(!browser.IPHONE && !browser.ITOUCH && !browser.IPAD && !browser.SAFARI){
				nodeList.lottery.style.transform = "translate3d(0,-10rem,0);" 
			}
		}
	}
    //-------------一切从这开始--------------
    var init = function(_data) {
        data = _data;
        // 根据数据初始化模块
        // opts["render"]({ "title": data["title"] });

        // 找到所有带有node-name的节点
        nodeList = parseModule(node);
        // 子模块实例化
        initMod();
        // 绑定事件
        bindEvents();
    }

    //---------------暴露API----------------
    that.init = init;
    return that;
};